package com.freek.ScreenDrop;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.prefs.Preferences;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;


public class OptionsFrame extends JFrame
{

	private JPanel contentPane;
	private KeyComboField desktopKeyField;
	private KeyComboField windowKeyField;
	private KeyComboField regionKeyField;
	
	final JFileChooser fileChooser = new JFileChooser();
	
	public static KeyCombo desktopCombo = new KeyCombo(KeyEvent.VK_3,KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK);
	public static KeyCombo windowCombo = new KeyCombo(KeyEvent.VK_2,KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK);
	public static KeyCombo regionCombo = new KeyCombo(KeyEvent.VK_4,KeyEvent.CTRL_MASK|KeyEvent.SHIFT_MASK);


	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable() {
			public void run()
			{
				try
				{
					OptionsFrame frame = new OptionsFrame();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public OptionsFrame()
	{
		
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new GridLayout(1, 0, 0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane);
		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Key Combos", null, panel_1, null);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblDesktop_1 = new JLabel("Capture Desktop");
		panel_1.add(lblDesktop_1, "2, 2, right, default");
		
		
		desktopKeyField = new KeyComboField(desktopCombo);
		desktopKeyField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
			}
		});
		panel_1.add(desktopKeyField, "4, 2, fill, default");
		desktopKeyField.setColumns(10);
		
		JLabel lblWindow_1 = new JLabel("Capture Window");
		panel_1.add(lblWindow_1, "2, 4, right, default");
		
		windowKeyField = new KeyComboField(windowCombo);
		windowKeyField.setColumns(10);
		panel_1.add(windowKeyField, "4, 4, fill, default");
		
		JLabel lblRegion_1 = new JLabel("Capture Region");
		panel_1.add(lblRegion_1, "2, 6, right, default");
		
		regionKeyField = new KeyComboField(regionCombo);
		regionKeyField.setColumns(10);
		panel_1.add(regionKeyField, "4, 6, fill, default");
		
		//load saved data
		loadPreferences();
		
		FocusListener saveListener = new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e)
			{
				super.focusLost(e);
				savePreferences();
			}
		};
		desktopKeyField.addFocusListener(saveListener);
		windowKeyField.addFocusListener(saveListener);
		regionKeyField.addFocusListener(saveListener);
		
	}
	
	public static final String DESKTOP_PATH = "DESKTOP_PATH";
	public static final String WINDOW_PATH = "WINDOW_PATH";
	public static final String REGION_PATH = "REGION_PATH";
	public static final String DESKTOP_COMBO = "DESKTOP_COMBO";
	public static final String WINDOW_COMBO = "WINDOW_COMBO";
	public static final String REGION_COMBO = "REGION_COMBO";
	
	private void savePreferences()
	{
		Preferences prefs = Preferences.userNodeForPackage(this.getClass());
		desktopKeyField.getCombo().put(prefs, DESKTOP_COMBO);
		windowKeyField.getCombo().put(prefs, WINDOW_COMBO);
		regionKeyField.getCombo().put(prefs, REGION_COMBO);
	}
	
	private void loadPreferences()
	{
		Preferences prefs = Preferences.userNodeForPackage(this.getClass());
		desktopCombo = KeyCombo.get(prefs, DESKTOP_COMBO, desktopCombo);
		desktopKeyField.setCombo(desktopCombo);
		windowCombo = KeyCombo.get(prefs, WINDOW_COMBO, windowCombo);
		windowKeyField.setCombo(windowCombo);
		regionCombo = KeyCombo.get(prefs, REGION_COMBO, regionCombo);
		regionKeyField.setCombo(regionCombo);
	}
	
	
}
